﻿namespace Sc.Domain
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public struct RefDataRecord
    {
        private string prefix;
        private int objectId;
        private string objectName;
        private string url;

        /// <summary>
        /// Gets or sets the prefix.
        /// </summary>
        /// <value>The prefix.</value>
        public string Prefix
        {
            get { return prefix; }
            set { prefix = value; }
        }

        /// <summary>
        /// Gets or sets the object id.
        /// </summary>
        /// <value>The object id.</value>
        public int ObjectId
        {
            get { return objectId; }
            set { objectId = value; }
        }

        /// <summary>
        /// Gets or sets the name of the object.
        /// </summary>
        /// <value>The name of the object.</value>
        public string ObjectName
        {
            get { return objectName; }
            set { objectName = value; }
        }

        /// <summary>
        /// 链接地址
        /// </summary>
        /// <value>The URL.</value>
        public string Url
        {
            get { return url; }
            set { url = value; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="RefDataRecord"/> struct.
        /// </summary>
        /// <param name="prefix">The prefix.</param>
        /// <param name="objectId">The object id.</param>
        public RefDataRecord(string prefix, int objectId)
        {
            this.prefix   = prefix;
            this.objectId = objectId;
            this.objectName = string.Empty;
            this.url = string.Empty;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="RefDataRecord"/> struct.
        /// </summary>
        /// <param name="prefix">The prefix.</param>
        /// <param name="objectId">The object id.</param>
        public RefDataRecord(string prefix, int objectId, string objectName)
        {
            this.prefix = prefix;
            this.objectId = objectId;
            this.objectName = objectName;
            this.url = string.Empty;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="RefDataRecord"/> struct.
        /// </summary>
        /// <param name="prefix">The prefix.</param>
        /// <param name="objectId">The object id.</param>
        /// <param name="objectName">Name of the object.</param>
        /// <param name="url">The URL.</param>
        public RefDataRecord(string prefix, int objectId, string objectName, string url)
        {
            this.prefix = prefix;
            this.objectId = objectId;
            this.objectName = objectName;
            this.url = url;
        }

        /// <summary>
        /// 实例化(前缀+ID)
        /// </summary>
        /// <param name="strDataRecord">前缀+ID</param>
        public RefDataRecord(string strDataRecord)
        {
            if (strDataRecord.Length <= 4) throw new ArgumentException("Argument Invalid. Prefix + ObjectId ");
            this.prefix   = strDataRecord.Substring(0, 4);
            this.objectId = int.Parse(strDataRecord.Substring(4));
            this.objectName = string.Empty;
            this.url = string.Empty;
        }

        /// <summary>
        /// Returns the fully qualified type name of this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> containing a fully qualified type name.
        /// </returns>
        public override string ToString()
        {
            if (string.IsNullOrEmpty(prefix) || objectId < 0) return string.Empty;
            if (prefix.Length < 4) throw new ArgumentException("Argument Invalid. Prefix ");
            return string.Concat(prefix, objectId);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            //Check for null and compare run-time types.
            if (obj == null || GetType() != obj.GetType()) return false;
            RefDataRecord r = (RefDataRecord)obj;
            return (prefix == r.prefix) && (objectId == r.objectId);
        }

        /// <summary>
        /// Implements the operator ==.
        /// </summary>
        /// <param name="left">The left.</param>
        /// <param name="right">The right.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(RefDataRecord left, RefDataRecord right)
        {
            return ((left.prefix == right.prefix) && (left.objectId == right.objectId));
        }

        /// <summary>
        /// Implements the operator !=.
        /// </summary>
        /// <param name="left">The left.</param>
        /// <param name="right">The right.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(RefDataRecord left, RefDataRecord right)
        {
            return ((left.prefix != right.prefix) || (left.objectId != right.objectId));
        }

    }
}
